<template>
  <div class="history-images">
    <div class="component-title">
      <dv-decoration-3 style="width:200px;height:20px;" />
      <span></span>
    </div>

    <div class="images-content">
      <div class="filter-bar">
        <dv-border-box-13>
          <div class="filter-content">
            <el-date-picker
              v-model="dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              size="small"
              :shortcuts="dateShortcuts"
            />
            <el-select v-model="timeSlot" placeholder="时间段" size="small">
              <el-option label="全天" value="all" />
              <el-option label="白天" value="day" />
              <el-option label="夜间" value="night" />
            </el-select>
          </div>
        </dv-border-box-13>
      </div>

      <div class="images-grid">
        <div v-for="item in imageList" :key="item.id" class="image-item">
          <dv-border-box-13>
            <div class="image-wrapper">
              <el-image 
                :src="item.url" 
                fit="cover"
                :preview-src-list="[item.url]"
                :initial-index="0"
              >
                <template #error>
                  <div class="image-error">
                    <el-icon><Picture /></el-icon>
                    <span>加载失败</span>
                  </div>
                </template>
                <template #placeholder>
                  <div class="image-loading">
                    <el-icon><Loading /></el-icon>
                    <span>加载中...</span>
                  </div>
                </template>
              </el-image>
              <div class="image-info">
                <div class="info-left">
                  <el-icon><Timer /></el-icon>
                  <span class="image-time">{{ item.time }}</span>
                </div>
                <div class="info-right">
                  <el-button 
                    type="primary" 
                    size="small" 
                    @click="downloadImage(item)"
                  >
                    <el-icon><Download /></el-icon>
                    下载
                  </el-button>
                </div>
              </div>
            </div>
          </dv-border-box-13>
        </div>
      </div>

      <div class="pagination-bar">
        <dv-border-box-13>
          <div class="pagination-content">
            <el-pagination
              v-model:current-page="currentPage"
              v-model:page-size="pageSize"
              :total="total"
              :page-sizes="[12, 24, 36, 48]"
              layout="total, sizes, prev, pager, next"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </dv-border-box-13>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Picture, Loading, Timer, Download } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const dateRange = ref([])
const timeSlot = ref('all')
const currentPage = ref(1)
const pageSize = ref(12)
const total = ref(100)

const dateShortcuts = [
  {
    text: '最近一周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    }
  },
  {
    text: '最近一月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    }
  }
]

// 模拟图片数据
const imageList = ref([
  {
    id: 1,
    url: 'https://example.com/image1.jpg',
    time: '2024-01-10 10:30:00'
  },
  {
    id: 2,
    url: 'https://example.com/image2.jpg',
    time: '2024-01-10 11:30:00'
  },
  // ... 更多图片数据
])

const handleSizeChange = (val) => {
  pageSize.value = val
  fetchImages()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchImages()
}

const downloadImage = (image) => {
  try {
    const link = document.createElement('a')
    link.href = image.url
    link.download = `监控画面_${image.time}.jpg`
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    ElMessage.success('下载成功')
  } catch (error) {
    ElMessage.error('下载失败')
  }
}

const fetchImages = () => {
  // 根据筛选条件获取图片列表
}

onMounted(() => {
  fetchImages()
})
</script>

<style scoped>
.history-images {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.component-title {
  height: 40px;
  display: flex;
  align-items: center;
  padding: 0 10px;
}

.component-title span {
  margin-left: 10px;
  font-size: 18px;
  color: #7ec699;
}

.images-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 15px;
  padding: 10px;
  min-height: 0;
}

.filter-bar {
  height: 50px;
}

.filter-bar :deep(.dv-border-box-13) {
  width: 100%;
  height: 100%;
}

.filter-content {
  height: 100%;
  padding: 0 10px;
  display: flex;
  align-items: center;
  gap: 15px;
}

.images-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
  overflow-y: auto;
  padding-right: 5px;
}

.images-grid::-webkit-scrollbar {
  width: 6px;
}

.images-grid::-webkit-scrollbar-thumb {
  background: #7ec699;
  border-radius: 3px;
}

.images-grid::-webkit-scrollbar-track {
  background: rgba(126, 198, 153, 0.1);
  border-radius: 3px;
}

.image-item {
  aspect-ratio: 4/3;
}

.image-item :deep(.dv-border-box-13) {
  width: 100%;
  height: 100%;
}

.image-wrapper {
  height: 100%;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.el-image {
  flex: 1;
  border-radius: 4px;
  overflow: hidden;
  background: #1a1a1a;
}

.image-error,
.image-loading {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 10px;
  color: #7ec699;
  font-size: 14px;
}

.image-error .el-icon,
.image-loading .el-icon {
  font-size: 24px;
}

.image-info {
  height: 32px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.info-left {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #7ec699;
}

.image-time {
  font-size: 12px;
}

.info-right :deep(.el-button) {
  display: flex;
  align-items: center;
  gap: 5px;
}

.pagination-bar {
  height: 50px;
}

.pagination-bar :deep(.dv-border-box-13) {
  width: 100%;
  height: 100%;
}

.pagination-content {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

:deep(.el-pagination) {
  --el-pagination-bg-color: transparent;
  --el-pagination-text-color: #7ec699;
  --el-pagination-button-color: #7ec699;
  --el-pagination-hover-color: #fff;
}

:deep(.el-date-editor),
:deep(.el-select) {
  --el-select-border-color-hover: #7ec699;
  --el-input-hover-border-color: #7ec699;
  --el-input-focus-border-color: #7ec699;
}
</style> 